package playsManager.gui;

import java.awt.Dialog.ModalityType;
import java.awt.event.*;

import javax.swing.*;

import playsManager.*;

class Logon {

	public static LogonInfo PromptForLogonInfo()
	{
		Logon logon = new Logon();
		logon.Show();
			
		if(logon.okButtonClicked)
		{
			LogonInfo result = new LogonInfo();
			result.username = logon.username;
			result.password = logon.password;
			return result;
		}
		else
		{
			return null;
		}
	}
	
	JDialog logonDialog;
	JTextField usernameField;
	JTextField passwordField;
	JButton okButton;
	JButton cancelButton;
	public boolean okButtonClicked;
	public String username;
	public String password;
	
	public Logon()
	{
		logonDialog = new JDialog();
		logonDialog.setModalityType(ModalityType.APPLICATION_MODAL);
		
		JPanel logonPanel = new JPanel();
		logonDialog.add(logonPanel);
		
		BoxLayout boxLayout = new BoxLayout(logonPanel, BoxLayout.Y_AXIS);
		logonPanel.setLayout(boxLayout);
		
		JPanel fieldsPane = new JPanel();
		BoxLayout boxLayout2 = new BoxLayout(fieldsPane, BoxLayout.Y_AXIS);
		fieldsPane.setLayout(boxLayout2);
		
		logonPanel.add(fieldsPane);
		
		JPanel buttonsPane = new JPanel();
		logonPanel.add(buttonsPane);
		
		JPanel usernamePane = new JPanel();
		fieldsPane.add(usernamePane);
		
		JPanel passwordPane = new JPanel();
		fieldsPane.add(passwordPane);
		
		JLabel usernameLabel = new JLabel("username:");
		usernamePane.add(usernameLabel);
		
		usernameField = new JTextField(20);
		usernamePane.add(usernameField);
		
		JLabel passwordLabel = new JLabel("password:");
		passwordPane.add(passwordLabel);
		
		passwordField = new JPasswordField(20);
		passwordPane.add(passwordField);
		
		okButton = new JButton("log in");
		buttonsPane.add(okButton);
		okButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				okButtonClicked = true; //indicate that the okay button was clicked
				username = usernameField.getText();
				password = passwordField.getText();
				logonDialog.setVisible(false); //close the logon window
			}
		});
		
		cancelButton = new JButton("cancel");
		buttonsPane.add(cancelButton);
		cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				okButtonClicked = false; //indicate that the cancel button was clicked
				logonDialog.setVisible(false); //close the logon window
			}
		});
		
		
		logonDialog.pack();
	}
	
	public void Show()
	{
		logonDialog.setVisible(true);	
	}
	
	
	
	//loginSignal.countDown();
}
